import {createRouter, createWebHashHistory} from 'vue-router'
import {UserType, STORE, isLogin} from "@/scripts/account";

// 引入页面组件
import Home from "@/components/Home";
import Login from "@/components/Login";
import Admin from "@/components/admin/Admin";
import Driver from "@/components/driver/Driver";
import Operator from "@/components/operator/Operator";
import Booking from "@/components/util/booking/Booking";
import AdminMoney from "@/components/admin/AdminMoney";
import AdminCompany from "@/components/admin/AdminCompany";
import AdminStatistic from "@/components/admin/AdminStatistic";
import AdminCompanyOperator from "@/components/admin/AdminCompanyOperator";
import AdminCompanyDriver from "@/components/admin/AdminCompanyDriver";
import AdminCompanyWarehouse from "@/components/admin/AdminCompanyWarehouse";
import AdminCompanyTruck from "@/components/admin/AdminCompanyTruck";
import BookingCheckIn from "@/components/util/booking/BookingCheckIn";
import Transport from "@/components/util/transport/Transport";
import {getBookingList, getBookingListByOperator, getBookingListByWarehouse} from "@/scripts/data/booking";
import {getTransportList, getTransportListByDriver} from "@/scripts/data/transport";


// 路由配置
const routes = [
    {name: 'home', path: '/', component: Home},
    {name: 'login', path: '/login', component: Login},
    {
        name: 'admin',
        path: UserType.properties[UserType.ADMIN].url,
        redirect: {name: 'admin-booking'},
        component: Admin,
        children: [
            {
                name: 'admin-booking', path: 'booking', component: Booking, props: {
                    pageTitle: '货单管理页面',
                    isModifiable: true,
                    isViewable: true,
                    isCheckable: true,
                    dataFetchMethod: getBookingList
                }
            },
            {
                name: 'admin-transport', path: 'transport', component: Transport, props: {
                    pageTitle: '运单管理页面', adminMod: true, dataFetchMethod: getTransportList
                }
            },
            {name: 'admin-money', path: 'money', component: AdminMoney},
            {
                name: 'admin-company',
                path: 'company',
                redirect: {name: 'admin-company-operator'},
                component: AdminCompany,
                children: [
                    {name: 'admin-company-operator', path: 'operator', component: AdminCompanyOperator},
                    {name: 'admin-company-driver', path: 'driver', component: AdminCompanyDriver},
                    {name: 'admin-company-warehouse', path: 'warehouse', component: AdminCompanyWarehouse},
                    {name: 'admin-company-truck', path: 'truck', component: AdminCompanyTruck},
                ]
            },
            {name: 'admin-statistic', path: 'statistic', component: AdminStatistic},
        ]
    },
    {
        name: 'driver', path: UserType.properties[UserType.DRIVER].url, component: Driver,
        redirect: {name: 'driver-transport'},
        children: [
            {
                name: 'driver-transport', path: 'transport', component: Transport, props: {
                    pageTitle: '查看运输任务', adminMod: false, dataFetchMethod: getTransportListByDriver
                }
            }
        ]
    },
    {
        name: 'operator',
        path: UserType.properties[UserType.OPERATOR].url,
        redirect: {name: 'operator-add'},
        component: Operator,
        children: [
            {name: 'operator-add', path: 'add', component: BookingCheckIn},
            {
                name: 'operator-view', path: 'view', component: Booking, props: {
                    pageTitle: '查看已添加货单页面',
                    isModifiable: false,
                    isViewable: true,
                    isCheckable: false,
                    dataFetchMethod: getBookingListByOperator
                }
            },
            {
                name: 'operator-check', path: 'check', component: Booking, props: {
                    pageTitle: '验收货单页面',
                    isModifiable: false,
                    isViewable: false,
                    isCheckable: true,
                    dataFetchMethod: getBookingListByWarehouse
                }
            },
        ]
    },
]

// 创建路由
export const router = createRouter({
    history: createWebHashHistory(),
    routes
})

// 导航守卫
// TODO 存在问题，假如登录就可以访问任意页面
router.beforeEach(async (to) => {
    if (!isLogin() && to.name !== 'home' && to.name !== 'login') {
        return {name: 'login', params: {isGoingBack: true}}
    } else {
        return true
    }
})

export const goHome = () => {
    router.push({name: 'home'})
}

export const goToUserHome = () => {
    router.push(UserType.properties[STORE.userState.type].url)
}

export const goToPage = (pageName) => {
    router.push({name: pageName})
}
